package v1

import (
	"gin-vue-admin/global"
    "gin-vue-admin/model"
    "gin-vue-admin/model/request"
    "gin-vue-admin/model/response"
    "gin-vue-admin/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

// @Tags UserOrder
// @Summary 创建UserOrder
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.UserOrder true "创建UserOrder"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /userOrder/createUserOrder [post]
func CreateUserOrder(c *gin.Context) {
	var userOrder model.UserOrder
	_ = c.ShouldBindJSON(&userOrder)
	if err := service.CreateUserOrder(userOrder); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags UserOrder
// @Summary 删除UserOrder
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.UserOrder true "删除UserOrder"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /userOrder/deleteUserOrder [delete]
func DeleteUserOrder(c *gin.Context) {
	var userOrder model.UserOrder
	_ = c.ShouldBindJSON(&userOrder)
	if err := service.DeleteUserOrder(userOrder); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// @Tags UserOrder
// @Summary 批量删除UserOrder
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除UserOrder"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /userOrder/deleteUserOrderByIds [delete]
func DeleteUserOrderByIds(c *gin.Context) {
	var IDS request.IdsReq
    _ = c.ShouldBindJSON(&IDS)
	if err := service.DeleteUserOrderByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// @Tags UserOrder
// @Summary 更新UserOrder
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.UserOrder true "更新UserOrder"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /userOrder/updateUserOrder [put]
func UpdateUserOrder(c *gin.Context) {
	var userOrder model.UserOrder
	_ = c.ShouldBindJSON(&userOrder)
	for k,_:= range userOrder.UserOrderDetail{
		userOrder.UserOrderDetail[k].ID = 0
	}
	for k,_:= range userOrder.UserOrderSendPic{
		userOrder.UserOrderSendPic[k].ID = 0
	}
	for k,_:= range userOrder.UserOrderGetPic{
		userOrder.UserOrderGetPic[k].ID = 0
	}
	for k,_:= range userOrder.UserOrderPay{
		userOrder.UserOrderPay[k].ID = 0
	}
	service.DeleteUserOrderPayByOrderID(int(userOrder.ID))
	service.DeleteUserOrderDetailByOrderId(int(userOrder.ID))
	service.DeleteUserOrderSendByOrderId(int(userOrder.ID))
	service.DeleteUserOrderGetPicByOrderId(int(userOrder.ID))
	if err := service.UpdateUserOrder(userOrder); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags UserOrder
// @Summary 用id查询UserOrder
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.UserOrder true "用id查询UserOrder"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /userOrder/findUserOrder [get]
func FindUserOrder(c *gin.Context) {
	var userOrder model.UserOrder
	_ = c.ShouldBindQuery(&userOrder)
	if err, reuserOrder := service.GetUserOrder(userOrder.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reuserOrder": reuserOrder}, c)
	}
}

// @Tags UserOrder
// @Summary 分页获取UserOrder列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.UserOrderSearch true "分页获取UserOrder列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /userOrder/getUserOrderList [get]
func GetUserOrderList(c *gin.Context) {
	var pageInfo request.UserOrderSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := service.GetUserOrderInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}
